package com.example.mainactivity.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.example.mainactivity.R;
import com.example.mainactivity.utils.StringUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.example.mainactivity.api.ApiConfig.BASE_URl;

public class LoginActivity extends AppCompatActivity {
    private EditText account;
    private EditText pwd;
    private Button loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        account = findViewById(R.id.et_account);
        pwd = findViewById(R.id.et_pwd);
        loginBtn = findViewById(R.id.btn_login);
        loginBtn.setOnClickListener(v -> {
            String accoutStr = account.getText().toString().trim();
            String pwdStr = pwd.getText().toString().trim();
            login(accoutStr, pwdStr);
        });
    }

    private void login(String account, String pwd) {
        if (StringUtils.isEmpty(account)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_LONG).show();
        }
        if (StringUtils.isEmpty(pwd)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
        }
        //进行登入请求
        //1、创建okhttp 并封装成json发送出去
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Map map = new HashMap<>();
        map.put("mobile", account);
        map.put("password", pwd);
        String jsonStr = new JSONObject(map).toString();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);
        //2、创建request
        Request request = new Request.Builder()
                .url(BASE_URl + "/app/login")
                .addHeader("contentType", "application/json;charset=utf-8")
                .post(requestBody)
                .build();
        //3、创建call回调对象
        Call call = okHttpClient.newCall(request);
        //4、发起请求
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("login", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Log.e("login", s);
                //保存在ttit.xml
                SharedPreferences sp = getSharedPreferences("ttit",MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                //键值对方式存储
                edit.putString("token","123456789");
                //事务提交
                edit.commit();
                //在子线程中使用组件ui会报错
//                Looper.prepare();
//                Toast.makeText(LoginActivity.this,s,Toast.LENGTH_LONG).show();;
//                Looper.loop();
            }
        });
    }
}